user  nginx nginx;
worker_processes  6;
pid /var/run/nginx.pid;

events {
  worker_connections  1024;
  use epoll; # linux only!
}

http {
  # global passenger settings
  passenger_root <%= `/usr/bin/passenger-config --root`.strip %>;
  passenger_default_user app;
  passenger_pool_idle_time 0;
  rails_framework_spawner_idle_time 0;
  rails_app_spawner_idle_time 0;

  # We leave passenger_max_pool_size at the default for now. (It might
  # eventually be an ERB variable that can be set automatically based on the
  # instance type and what roles it's in.)
  # passenger_max_pool_size 6;

  # We leave the rails_spawn_method at the default, but we might want a config setting
  # to allow it to be changed.
  # rails_spawn_method smart-lv2;
  
  include /etc/nginx/mime.types;
  
  # set a default type for the rare situation that
  # nothing matches from the mime-type include
  default_type  application/octet-stream;

  # configure log format
  log_format main '$remote_addr [$time_local] '
                  '"$scheme $host $request" $status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" '
                  '($request_time)';
  
  access_log  /mnt/log/nginx/access.log  main;

  # main error log - Do not comment out. If you do not want the log file set this to /dev/null
  # use debug instead of notice if you want additional information
  error_log  /mnt/log/nginx/error.log notice;

  sendfile on;

  tcp_nopush        on;
  tcp_nodelay       on;
  gzip              on;
  gzip_http_version 1.1;
  gzip_vary on;
  gzip_comp_level   6;
  gzip_proxied      any;
  gzip_types        application/json application/x-javascript application/xhtml+xml application/xml application/xml+rss text/css text/javascript text/plain text/xml ;
  # make sure gzip does not lose large gzipped js or css files
  # see http://blog.leetsoft.com/2007/7/25/nginx-gzip-ssl
  gzip_buffers 16 8k;

  # Disable gzip for certain browsers. IE6 prior to SP2 doesn't handle gzip properly.
  gzip_disable “MSIE [1-6].(?!.*SV1)”;
  
  server {
    listen <%= roles[:proxy] && roles[:proxy].include?("127.0.0.1")  ? 81 : 80 %>;
    server_name _;
    
    # server-specific passenger settings
    passenger_enabled on;
    passenger_use_global_queue on;
    rails_env <%= rails_env %>;

    # Set the max size for file uploads to 50Mb
    client_max_body_size 50M;

    # uncomment to force a redirect to www
    # if ($host ~* "^[ec2onrails].com$"){
    #   rewrite ^(.*)$ http://www.[ec2onrails].com$1 permanent;
    #   break;
    # }
    
    # uncomment if you want to allow or force some or all pages to go to http:// instead of https://
    # if redirecting all to https, you won't need any of the other directives below their rewrite/break
    # set $sub 'www';
    # if  ($host ~* "^(.+?)\.[ec2onrails].com$"){
    #        set $sub $1;
    # }
    # 
    # if ( $uri ~* "^/.+$") {
    #    rewrite ^(.*)$ https://$sub.[ec2onrails].com$1 permanent;
    #    break;
    # }
    
    root /mnt/app/current/public;

    error_page   400 /400.html;
    error_page   500 502 504  /500.html;
    location = /500.html {
      root /mnt/app/current/public;
    }
    
    #hide hidden files and folders
    location ~ /\..+ {
      deny  all;
    }
    
    #do not show the nginx version number in the server header
    server_tokens off;
    
    # this allows people to use images and css in their maintenance.html file
    if ($request_filename ~* \.(css|jpg|gif|png)$) {
	    break;
    }

    # this allows the /nginx_status page to work, even if the maintenance
    # page is up.
    if ($uri ~* "^/nginx_status") {
	    break;
    }

    # this allows a url /warmup that can be used to cause Passenger to
    # restart after restart.txt has been touched, even if the maintenance
    # page is up.
    if ($uri ~* "^/warmup") {
	    break;
    }
		
    # this rewrites all the requests to the maintenance.html
    # page if it exists in the doc root. This is for capistrano's
    # disable web task
    if (-f $document_root/system/maintenance.html) {
      return 503;
    }
    error_page 503 @503;
    location @503 {
      rewrite  ^(.*)$  /system/maintenance.html break;
    }
    
    
    # see http://wiki.codemongers.com/NginxHttpStubStatusModule
    # for more information
    location /nginx_status {
        # copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/
        stub_status on;
        access_log   off;
        #only allow from localhost
        allow 127.0.0.1;
        deny all;
    }
    
    include /etc/nginx/conf.d/*.conf;
  }
  
  # This server is setup for ssl. Uncomment if 
  # you are using ssl as well as port 80.
  # server {
  #   # port to listen on. Can also be set to an IP:PORT
  #   listen 443;
  #
  #   ssl                  on;
  #   ssl_certificate      /etc/nginx/your_cert.crt;
  #   ssl_certificate_key  /etc/nginx/your_cert.key;
  #
  #   TODO SSL support
  #
  # }
}
